package com.dhcc.bpm.modules.sandbox.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.sandbox.entity.SandboxCaseDict;
import com.dhcc.bpm.modules.sandbox.service.ISandboxCaseDictService;
import com.dhcc.bpm.modules.sandbox.vo.SandboxCaseDictVO;
import com.dhcc.bpm.modules.sandbox.vo.TestDictTypeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SandboxCaseDictController
 * @Description 沙箱案例测试关联表 控制器
 * @Author sjm
 * @Date 2020/2/21 0021 15:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@AllArgsConstructor
@RequestMapping("bpm-sandbox/sandboxcasedict")
@Api(value = "沙箱案例测试关联表", tags = "沙箱案例测试关联表接口")
public class SandboxCaseDictController extends BladeController {
	private final static Logger log = LoggerFactory.getLogger(SandboxCaseDictController.class);
	private ISandboxCaseDictService sandboxCaseDictService;

	/**
	 * @Description 详情
	 * @Author sjm
	 * @Date 21:34 2020/2/21 0021
	 * @param sandboxCaseDict
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.sandbox.entity.SandboxCaseDict>
	 **/
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入sandboxCaseDict")
	@ApiLog("沙箱案例测试关联详情")
	@Log(title = "沙箱案例测试关联详情", businessType = BusinessType.SELECT)
	public R<SandboxCaseDict> detail(SandboxCaseDict sandboxCaseDict) {
		SandboxCaseDict detail = sandboxCaseDictService.getOne(Condition.getQueryWrapper(sandboxCaseDict));
		return R.data(detail);
	}

	/**
	 * @Description 分页 沙箱案例测试关联表
	 * @Author sjm
	 * @Date 21:34 2020/2/21 0021
	 * @param sandboxCaseDict
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.sandbox.entity.SandboxCaseDict>>
	 **/
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入sandboxCaseDict")
	@ApiLog("沙箱案例测试关联列表")
	@Log(title = "沙箱案例测试关联列表", businessType = BusinessType.SELECT)
	public R<IPage<SandboxCaseDict>> list(@ApiIgnore @RequestParam Map<String, Object> sandboxCaseDict, Query query) {
		IPage<SandboxCaseDict> pages = sandboxCaseDictService.page(Condition.getPage(query), Condition.getQueryWrapper(sandboxCaseDict, SandboxCaseDict.class));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 沙箱案例测试关联表
	 * @Author sjm
	 * @Date 21:34 2020/2/21 0021
	 * @param sandboxCaseDict
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.sandbox.vo.SandboxCaseDictVO>>
	 **/
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入sandboxCaseDict")
	@ApiLog("沙箱案例测试关联列表")
	@Log(title = "沙箱案例测试关联列表", businessType = BusinessType.SELECT)
	public R<IPage<SandboxCaseDictVO>> page(SandboxCaseDictVO sandboxCaseDict, Query query) {
		IPage<SandboxCaseDictVO> pages = sandboxCaseDictService.selectSandboxCaseDictPage(Condition.getPage(query), sandboxCaseDict);
		return R.data(pages);
	}

	/**
	 * 自定义分页 沙箱案例测试关联表
	 */
	/**
	 * @Description 自定义分页 沙箱案例测试关联表
	 * @Author sjm
	 * @Date 21:34 2020/2/21 0021
	 * @param testDictTypeVO
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.sandbox.vo.TestDictTypeVO>>
	 **/
	@GetMapping("/fetchListByCaseId")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "分页", notes = "传入sandboxCaseDict")
	@ApiLog("沙箱案例测试关联列表")
	@Log(title = "沙箱案例测试关联列表", businessType = BusinessType.SELECT)
	public R<IPage<TestDictTypeVO>> fetchListByCaseId(TestDictTypeVO testDictTypeVO, Query query) {
		IPage<TestDictTypeVO> pages = sandboxCaseDictService.selectSandboxCaseByCaseId(Condition.getPage(query), testDictTypeVO);
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页 沙箱案例测试关联表
	 * @Author sjm
	 * @Date 21:34 2020/2/21 0021
	 * @param testDictTypeVO
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.sandbox.vo.TestDictTypeVO>>
	 **/
	@GetMapping("/fetchByCaseId")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "分页", notes = "传入sandboxCaseDict")
	@ApiLog("沙箱案例测试关联列表")
	@Log(title = "沙箱案例测试关联列表", businessType = BusinessType.SELECT)
	public R<IPage<TestDictTypeVO>> fetchByCaseId(TestDictTypeVO testDictTypeVO, Query query) {
		IPage<TestDictTypeVO> pages = sandboxCaseDictService.selectByCaseId(Condition.getPage(query), testDictTypeVO);
		return R.data(pages);
	}

	/**
	 * @Description 新增 沙箱案例测试关联表
	 * @Author sjm
	 * @Date 21:34 2020/2/21 0021
	 * @param sandboxCaseDict
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/save")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增", notes = "传入sandboxCaseDict")
	@ApiLog("新增沙箱案例测试关联")
	@Log(title = "新增沙箱案例测试关联", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody SandboxCaseDict sandboxCaseDict) {
		return R.status(sandboxCaseDictService.save(sandboxCaseDict));
	}

	/**
	 * @Description 修改 沙箱案例测试关联表
	 * @Author sjm
	 * @Date 21:35 2020/2/21 0021
	 * @param sandboxCaseDict
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/update")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "修改", notes = "传入sandboxCaseDict")
	@ApiLog("修改沙箱案例测试关联")
	@Log(title = "修改沙箱案例测试关联", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody SandboxCaseDict sandboxCaseDict) {
		return R.status(sandboxCaseDictService.updateById(sandboxCaseDict));
	}

	/**
	 * @Description 新增或修改 沙箱案例测试关联表
	 * @Author sjm
	 * @Date 21:35 2020/2/21 0021
	 * @param sandboxCaseDict
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "新增或修改", notes = "传入sandboxCaseDict")
	@ApiLog("修改沙箱案例测试关联")
	@Log(title = "修改沙箱案例测试关联", businessType = BusinessType.UPDATE)
	public R submit(@Valid @RequestBody SandboxCaseDict sandboxCaseDict) {
		return R.status(sandboxCaseDictService.saveOrUpdate(sandboxCaseDict));
	}

	/**
	 * @Description 新增或修改 沙箱案例测试关联表
	 * @Author sjm
	 * @Date 21:35 2020/2/21 0021
	 * @param caseId
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submitByCaseId")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "新增或修改", notes = "传入sandboxCaseDict")
	@ApiLog("新增或修改沙箱案例测试关联")
	@Log(title = "新增或修改沙箱案例测试关联", businessType = BusinessType.UPDATE)
	@Transactional(rollbackFor = Exception.class)
	public R submitByCaseId(@ApiParam(value = "主键集合", required = true) @RequestParam Long caseId,
							@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		try {
			List<Long> tests = Func.toLongList(ids);
			for (Long testId : tests) {
				SandboxCaseDictVO sandboxCaseDict = sandboxCaseDictService.selectSandboxCaseById(caseId, testId);
				if (sandboxCaseDict == null) {
					sandboxCaseDict = new SandboxCaseDictVO();
				}
				sandboxCaseDict.setCaseId(caseId);
				sandboxCaseDict.setTestId(testId);
				sandboxCaseDictService.saveOrUpdate(sandboxCaseDict);
			}
		}catch (Exception e){
			log.error("批量添加数据异常:{}", e);
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return R.status(false);
		}
		return R.status(true);
	}

	/**
	 * @Description 删除 沙箱案例测试关联表
	 * @Author sjm
	 * @Date 21:35 2020/2/21 0021
	 * @param caseId
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/removeByCaseId")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "新增或修改", notes = "caseId,testIds")
	@ApiLog("删除沙箱案例测试关联")
	@Log(title = "删除沙箱案例测试关联", businessType = BusinessType.DELETE)
	public R removeByCaseId(@ApiParam(value = "主键集合", required = true) @RequestParam Long caseId,
							@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		int num = sandboxCaseDictService.removeByIds(caseId, ids);
		if(num > 0){
			return  R.status(true);
		}else{
			return  R.status(false);
		}
	}

	/**
	 * @Description 删除 沙箱案例测试关联表
	 * @Author sjm
	 * @Date 21:35 2020/2/21 0021
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/remove")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "删除", notes = "传入ids")
	@ApiLog("删除沙箱案例测试关联")
	@Log(title = "删除沙箱案例测试关联", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(sandboxCaseDictService.removeByIds(Func.toLongList(ids)));
	}

	
}
